using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace MindFair.HomeLeague.Bowling
{
    public class Tournament
    {
        internal Tournament(string name, DateTime startDate, League sponsoringLeague)
        {
            this.Name = name;
            this.StartDate = startDate;
            if (sponsoringLeague == null)
                throw new ArgumentNullException("sponsoringLeague", "A Tournament must have a valid Sponsoring Tournament.");
            _sponsoringLeague = sponsoringLeague;
            _members = new SortedList<string,Bowler>();
            _exposedMembers = new ReadOnlyCollection<Bowler>(_members.Values);
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value", "The Tournament Name can not be 'null'.");
                _name = value;
            }
        }
        private DateTime _startDate;
        public DateTime StartDate
        {
            get { return _startDate; }
            set
            {
                DateTime datePartOnly = new DateTime(value.Year, value.Month, value.Day);
                if (datePartOnly < DateTime.Today)
                    throw new ArgumentException("A Tournament can not start in the past.", "value");
                if (_startDate > new DateTime() && DateTime.Now >= _startDate)
                    throw new DeadlineExceededException(this.StartDate,
                        string.Format("The Start Date for the Tournament ({0}) has already passed so it cannot be changed.",
                        this.StartDate.ToShortDateString()));
                _startDate = datePartOnly;
            }
        }
        private League _sponsoringLeague;
        public League SponsoringLeague
        {
            get { return _sponsoringLeague; }
        }

        private SortedList<string, Bowler> _members;
        private ReadOnlyCollection<Bowler> _exposedMembers;
        public ReadOnlyCollection<Bowler> Members
        {
            get { return _exposedMembers; }
        }
        public void AddMember(Bowler newMember)
        {
            if (!_members.ContainsKey(newMember.FullName))
                _members.Add(newMember.FullName, newMember);
        }
    }
}
